<template>
  <div class="timer-list">
    <div class="header">
      <el-button type="primary" @click="handleAdd">
        创建定时任务
      </el-button>
    </div>

    <el-table :data="timers" v-loading="loading">
      <el-table-column prop="name" label="任务名称" />
      <el-table-column label="设备" width="200">
        <template #default="{ row }">
          {{ getDeviceName(row.deviceId) }}
        </template>
      </el-table-column>
      <el-table-column prop="intervalMinutes" label="执行时间" />
      <el-table-column label="目标状态" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.targetStatus"
            disabled
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="form.id ? '编辑定时任务' : '创建定时任务'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="form" label-width="100px">
        <el-form-item label="任务名称" required>
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="选择设备" required>
          <el-select v-model="form.deviceId" placeholder="选择设备">
            <el-option
              v-for="item in devices"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目标状态" required>
          <el-switch v-model="form.targetStatus" />
        </el-form-item>
        <el-form-item label="执行时间" required>
          <el-input v-model="form.intervalMinutes" placeholder="" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">
          确定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getTimerList,
  createTimer,
  updateTimer,
  deleteTimer
} from '@/api/timer'
import { getDeviceList } from '@/api/device'

const timers = ref([])
const devices = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const form = ref({
  name: '',
  deviceId: null,
  targetStatus: false,
  cronExpression: '',
  status: true
})

const loadTimers = async () => {
  loading.value = true
  try {
    timers.value = await getTimerList()
  } finally {
    loading.value = false
  }
}

const loadDevices = async () => {
  devices.value = await getDeviceList()
}

const getDeviceName = (deviceId) => {
  const device = devices.value.find(d => d.id === deviceId)
  return device ? device.name : '-'
}

const handleAdd = () => {
  form.value = {
    name: '',
    deviceId: null,
    targetStatus: false,
    cronExpression: '',
    status: true
  }
  dialogVisible.value = true
}

const handleEdit = (row) => {
  form.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该定时任务吗？')
    await deleteTimer(row.id)
    ElMessage.success('删除成功')
    await loadTimers()
  } catch (e) {
    if (e !== 'cancel') {
      console.error(e)
    }
  }
}

const handleStatusChange = async (row) => {
  try {
    await updateTimer(row.id, row)
    ElMessage.success('状态更新成功')
  } catch (e) {
    row.status = !row.status
  }
}

const handleSubmit = async () => {
  try {
    if (form.value.id) {
      await updateTimer(form.value.id, form.value)
      ElMessage.success('更新成功')
    } else {
      await createTimer(form.value)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await loadTimers()
  } catch (e) {
    console.error(e)
  }
}

onMounted(() => {
  loadTimers()
  loadDevices()
})
</script>

<style lang="scss" scoped>
.timer-list {
  .header {
    margin-bottom: 20px;
  }
}
</style> 